<template>
  <div id="app">
    <el-container>
      <el-aside width="200px">
        <el-input placeholder="输入省/市/行政区划机构名称" v-model="filterText"></el-input>
        <el-tree
          class="filter-tree"
          :data="treeData"
          :props="defaultProps"
          node-key="id"
          :default-expanded-keys="defaultTreeKey"
          :filter-node-method="filterNode"
          @node-click="handleNodeClick"
          :highlight-current="true"
          :expand-on-click-node="false"
          ref="tree2"
        ></el-tree>
      </el-aside>
      <el-main>
        <div v-show="isMainShow">
          <el-row>
            <el-col :span="3" :offset="18">
              <el-button type="primary" @click="addOrg">新建机构</el-button>
            </el-col>
            <el-col :span="3">
              <ep-delete
                :id="orgId"
                parmeId="orgId"
                url="/sys/org/deleteOrg"
                :init="true"
                @init="initTree"
              >
                <el-button type="danger">注销</el-button>
              </ep-delete>
            </el-col>
          </el-row>
          <el-form
            style="margin-top:20px"
            ref="ruleForm"
            :model="mechanismForm"
            label-width="100px"
            :rules="rulesOrg"
          >
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="机构名称" prop="orgName">
                  <el-input v-model="mechanismForm.orgName"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="机构代码" prop="orgenterCode">
                  <el-input v-model="mechanismForm.orgenterCode"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="机构简称" prop="shortName">
                  <el-input v-model="mechanismForm.shortName"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="行政区划" prop="regionCode">
                  <el-select
                    style="width:100%;"
                    v-model="mechanismForm.regionCode"
                    placeholder="请选择活动区域"
                  >
                    <el-option
                      v-for="item in orgOptions"
                      :key="item.data.id"
                      :label="item.data.orgName"
                      :value="item.data.id"
                    ></el-option>
                    <!-- <el-option label="下城区" value="162"></el-option> -->
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="联系人">
                  <el-input v-model="mechanismForm.linkman"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="机构负责人">
                  <el-input v-model="mechanismForm.leader"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="同级序号">
                  <el-input v-model="mechanismForm.orgOrder"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="邮政编码">
                  <el-input v-model="mechanismForm.zip"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="主管部门">
                  <el-input v-model="mechanismForm.superdept"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col>
                <el-form-item label="机构地址">
                  <el-input v-model="mechanismForm.orgAddr"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col>
                <el-form-item label="机构描述">
                  <el-input type="textarea" :rows="3" v-model="mechanismForm.orgDesc"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col>
                <el-form-item label="请选择险种">
                  <el-card class="box-card">
                    <div slot="header" class="clearfix">
                      <el-checkbox
                        :indeterminate="isIndeterminate"
                        v-model="checkAll"
                        @change="handleCheckAllChange"
                      >全选</el-checkbox>
                    </div>
                    <div style="margin: 15px 0;"></div>
                    <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
                      <el-checkbox v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox>
                    </el-checkbox-group>
                  </el-card>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item>
              <el-button type="primary" @click="addCreate('ruleForm')">{{addOrEdit}}</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div v-show="!isMainShow">
          <el-row>
            
            <el-col :span="3" :offset="18">
              <el-button
                :disabled="true"
                v-show="cityName.four"
                type="primary"
                @click="addCity"
              >{{cityName.four}}</el-button>
            </el-col>
            <el-col :span="3">
              <el-button v-show="fourShow" type="primary" @click="addDownOrg">新建下级机构</el-button>
            </el-col>
          </el-row>
          <el-form style="margin-top:20px;" ref="form" :model="cityForm" label-width="100px">
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item :label="cityName.one">
                  <el-select
                    :disabled="true"
                    style="width:100%;"
                    v-model="cityForm.orgName"
                    placeholder="请选择活动区域"
                  >
                    <el-option
                      v-for="item in cityNameOptions"
                      :key="item.data.id"
                      :label="item.data.orgName"
                      :value="item.data.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item :label="cityName.two">
                  <el-input v-model="cityForm.orgenterCode"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item :label="cityName.three">
                  <el-input v-model="cityForm.shortName"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="联系人">
                  <el-input v-model="cityForm.linkman"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="联系手机">
                  <el-input v-model="cityForm.tel"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="同级序号">
                  <el-input v-model="cityForm.orgOrder"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="主管部门">
                  <el-input v-model="cityForm.superdept"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col>
                <el-form-item label="描述">
                  <el-input type="textarea" v-model="cityForm.orgDesc"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item>
              <el-button type="primary" @click="saveCity">保存</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
const cityOptions = ["城镇职工基本养老保险", "失业保险", "工伤保险"];
export default {
  data() {
    return {
      q: 0,
      fourShow: false,
      filterText: "",
      isMainShow: true,
      addOrEdit: "保存",
      orgOptions: [],
      defaultTreeKey: ["1"],
      mechanismForm: {
        orgName: "",
        orgenterCode: "",
        shortName: "",
        regionCode: "",
        linkman: "",
        leader: "",
        orgOrder: "",
        zip: "",
        superdept: "",
        orgAddr: "",
        orgDesc: ""
      },
      rulesOrg: {
        orgName: [
          { required: true, message: "请输入机构名称", trigger: "blur" }
        ],
        orgenterCode: [
          { required: true, message: "请输入机构代码", trigger: "blur" }
        ],
        shortName: [
          { required: true, message: "请输入机构简称", trigger: "blur" }
        ],
        regionCode: [
          { required: true, message: "请输入行政区划", trigger: "blur" }
        ]
      },
      cityForm: {
        orgName: "",
        orgenterCode: "",
        shortName: "",
        linkman: "",
        tel: "",
        orgOrder: "",
        superdept: "",
        orgDesc: ""
      },
      cityNameOptions: [],
      checkAll: false,
      checkedCities: [],
      cities: cityOptions,
      isIndeterminate: true,
      cityName: {
        one: "省份名称",
        two: "省份代码",
        three: "省份简称",
        four: ""
      },
      parentId: "",
      orgId: "",
      id: "",
      treeData: [],
      defaultProps: {
        children: "children",
        label: "orgName"
      }
    };
  },
  watch: {
    filterText(val) {
      this.$refs.tree2.filter(val);
    }
  },
  created() {
    this.initTree();
  },
  methods: {
    initTree() {
      this.q = 0;
      this.$get("/sys/org/queryTree").then(data => {
        if (data.data.success) {
          this.treeData = data.data.result;
          let arr = data.data.result;
          let _this = this;
          function fn(arr) {
            for (var i = 0; i < arr.length; i++) {
              if (arr[i].children) {
                arr = arr[i].children;
                _this.q++;
                fn(arr);
              } else {
                return;
              }
            }
          }
          fn(arr);
        }
      });
    },
    addCreate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$post("/sys/org/saveOrg", {
            id: this.orgId,
            parentId: Number(this.mechanismForm.regionCode),
            orgName: this.mechanismForm.orgName,
            orgenterCode: this.mechanismForm.orgenterCode,
            shortName: this.mechanismForm.shortName,
            regionCode: this.mechanismForm.regionCode,
            linkman: this.mechanismForm.linkman,
            leader: this.mechanismForm.leader,
            orgOrder: this.mechanismForm.orgOrder,
            zip: this.mechanismForm.zip,
            superdept: this.mechanismForm.superdept,
            orgAddr: this.mechanismForm.orgAddr,
            orgDesc: this.mechanismForm.orgDesc
          }).then(data => {
            this.$mess(data);
            if (data.success) {
              this.initTree();
              this.defaultTreeKey = [];

              this.defaultTreeKey.push(this.orgId);
            }
          });
        } else {
          return false;
        }
      });
    },
    addOrg(num) {
      this.addOrEdit = "立即创建";
      for (let key in this.mechanismForm) {
        if (key == "regionCode") {
          this.mechanismForm.regionCode = this.parentId;
          continue;
        }
        this.mechanismForm[key] = "";
      }
      this.orgId = "";
      if (num) {
        this.mechanismForm.regionCode = this.id;
      }
    },
    addDownOrg() {
      this.isMainShow = true;
      this.addOrg(this.id);
    },
    addCity() {
      for (let key in this.cityForm) {
        this.cityForm[key] = "";
      }
    },
    saveCity() {
      this.$post("/sys/org/saveOrg", {
        id: Number(this.orgId),
        // parentId: Number(this.parentId),
        orgName: this.cityForm.orgName,
        orgenterCode: this.cityForm.orgenterCode,
        shortName: this.cityForm.shortName,
        regionCode: this.cityForm.regionCode,
        linkman: this.cityForm.linkman,
        leader: this.cityForm.leader,
        orgOrder: this.cityForm.orgOrder,
        zip: this.cityForm.zip,
        superdept: this.cityForm.superdept,
        orgAddr: this.cityForm.orgAddr,
        orgDesc: this.cityForm.orgDesc
      }).then(data => {
        this.$mess(data);
        if (data.success) {
          this.initTree();
          this.defaultTreeKey = [];
          this.defaultTreeKey.push(this.orgId);
        }
      });
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.orgName.indexOf(value) !== -1;
    },
    handleCheckAllChange(val) {
      this.checkedCities = val ? cityOptions : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.cities.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.cities.length;
    },
    handleNodeClick(data, node) {
      // this.$get("/sys/org/findOrgById", {
      //   orgId: data.id
      // }).then(data => {
      //
      // });
      this.parentId = node.parent.data.id;
      this.orgId = data.id;
      this.id = data.id;
      this.addOrEdit = "保存";
      var obj = Object.assign({}, data);
      this.mechanismForm = obj;
      // this.mechanismForm = data;

      this.cityNameOptions = node.parent.childNodes;

      // this.cityForm.orgName = data.label;
      this.cityForm = obj;
      if (node.level - this.q == -3) {
        this.isMainShow = false;
        this.cityName = { one: "省份名称", two: "省份代码", three: "省份简称" };
      }
      if (node.level - this.q == -2) {
        this.isMainShow = false;
        this.cityName = {
          one: "地市名称",
          two: "地市代码",
          three: "地市简称",
          four: "新建地市"
        };
      }
      if (node.level - this.q == -1) {
        this.isMainShow = false;
        this.cityName = {
          one: "行政区划名称",
          two: "行政区划代码",
          three: "行政区划简称",
          four: "新建行政区划"
        };
        this.fourShow = true;
      } else {
        this.fourShow = false;
      }
      if (node.level - this.q == 0) {
        this.isMainShow = true;
        this.cityName.four = "新建机构";
        this.orgOptions = node.parent.parent.childNodes;
      }
    }
  }
};
</script>

<style scoped lang="css">
</style>    
